/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package irsystem.scenario.method;

import irsystem.collection.CISI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Cerulean
 */
public class InverseDocumentFrequency {

    Set<String> words;
    CISI cisi;

    public InverseDocumentFrequency(List<String> queryArrays, CISI cisi) {
        words=new HashSet<String>();
        for(String str:queryArrays)
        {
            if(words.contains(str)==false)
            {
                words.add(str);
            }
        }
        this.cisi = cisi;
    }

    public TermVectorSpace process() {
        HashMap<String, Double> occurrencesMap = new HashMap<String, Double>();
        for (String word : words) {
            occurrencesMap.put(word, 0.0);
        }
        for (int i = 1; i < cisi.getDocs().size(); ++i) {
            String[] termArray = cisi.getDocumentByID(i).getWords().split("\\s+");
            for (String term : words) {
                for (int j = 0; j < termArray.length; ++j) {
                    if (term.equals(termArray[j])) {
                        double temp = occurrencesMap.get(term);
                        if(occurrencesMap.containsKey(term)) {
                            occurrencesMap.remove(term);
                        }
                        occurrencesMap.put(term, temp+1);
                        break;
                    }
                }
            }
        }
        TermVectorSpace idfVector = new TermVectorSpace(occurrencesMap);
        for(String term: words) {
            if(idfVector.getValue(term)==0)
            {
                idfVector.updateValue(term,0);
            }
            else
            {
                idfVector.updateValue(term, Math.log(cisi.getDocs().size()/idfVector.getValue(term)));
            }
        }
        return idfVector;
    }
}
